package com.yhsoft.bill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.bill.controller.request.SalerCzInVo;
import com.yhsoft.bill.controller.request.SalerCzMxInVo;
import com.yhsoft.bill.controller.response.SalerCzMxOutVo;
import com.yhsoft.bill.controller.response.SalerXhLogOutVo;
import com.yhsoft.bill.entity.SalerYfmx;
import com.yhsoft.bill.mapper.SalerYfmxMapper;
import com.yhsoft.bill.service.ISalerYfmxService;
import com.yhsoft.common.service.Po.TokenUser;
import com.yhsoft.common.service.TokenService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * <p>
 * 渠道商充值明细表 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-11-03
 */
@Service
public class SalerYfmxServiceImpl extends ServiceImpl<SalerYfmxMapper, SalerYfmx> implements ISalerYfmxService {

    @Override
    public String cz(SalerCzInVo inVo) {
        TokenUser user = TokenService.getUserBytoken();
        SalerYfmx yszkMx = new SalerYfmx();
        yszkMx.setCompanyId(user.getCompanyId());
        yszkMx.setPayBy(user.getName());
        yszkMx.setPayAmount(inVo.getPayAmount());
        yszkMx.setVoucher(inVo.getVoucher());
        yszkMx.setPayTime(LocalDateTime.now());
        this.baseMapper.insert(yszkMx);
        return "success";
    }

    @Override
    public IPage<SalerCzMxOutVo> getCzMx(SalerCzMxInVo inVo, Long pageNo, Long pageSize) {
        TokenUser user = TokenService.getUserBytoken();
        LambdaQueryWrapper<SalerYfmx> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SalerYfmx::getCompanyId, user.getCompanyId());
        wrapper.ge(inVo.getStartTime() != null, SalerYfmx::getPayTime, inVo.getStartTime());
        if (inVo.getEndTime() != null) {
            wrapper.le(SalerYfmx::getPayTime, LocalDateTime.of(inVo.getEndTime(), LocalTime.MAX));
        }

        wrapper.orderByDesc(SalerYfmx::getCreateTime);
        Page<SalerYfmx> page = new Page<>(pageNo, pageSize);
        IPage<SalerYfmx> iPage = baseMapper.selectPage(page, wrapper);

        IPage<SalerCzMxOutVo> convert = iPage.convert(SalerYfmx -> {
            SalerCzMxOutVo form = new SalerCzMxOutVo();
            BeanUtils.copyProperties(SalerYfmx, form);
            return form;
        });
        return convert;
    }

    @Override
    public IPage<SalerXhLogOutVo> getXhLog(SalerCzMxInVo inVo, Long pageNo, Long pageSize) {

        QueryWrapper<SalerXhLogOutVo> wrapper = new QueryWrapper<>();
        wrapper.eq("a.distributor_id", TokenService.getUserBytoken().getCompanyId());
        wrapper.eq("a.order_status", 0);
        wrapper.ne("a.order_type", "DY");

        // 查询条件
        wrapper.ge((inVo.getStartTime() != null), "a.pay_time", inVo.getStartTime());
        if (inVo.getEndTime() != null) {
            wrapper.le("a.pay_time", LocalDateTime.of(inVo.getEndTime(), LocalTime.MAX));
        }

        Page<SalerXhLogOutVo> page = new Page<>(pageNo, pageSize);
        IPage<SalerXhLogOutVo> iPage = this.baseMapper.getListPageByConditions(page, wrapper);

        return iPage;
    }
}
